package com.gzz.orchid.common.core.tuple;

import com.gzz.orchid.common.core.util.I18nUtil;
import lombok.Getter;

/**
 * 坐标
 *
 * @author higmd
 */
@Getter
public class Axis {
    // 地球半径(千米)
    private final static double R = 6371;
    // 经度
    private final double lng;
    // 维度
    private final double lat;
    // 构造函数
    public Axis(String lngAndLat) {
        String[] dests = lngAndLat.split(",");
        if (dests.length != 2) {
            throw new IllegalArgumentException(I18nUtil.ofMessage("坐标格式无效, 格式为：经度LNG,纬度LAT "));
        }
        try {
            this.lng = Double.parseDouble(dests[0]);
            this.lat = Double.parseDouble(dests[1]);
        } catch (Exception e) {
            throw new IllegalArgumentException(I18nUtil.ofMessage("坐标格式无效, 格式为：经度LNG,纬度LAT "));
        }
    }
    // 构造函数
    public Axis(double lng, double lat) {
        this.lng = lng;
        this.lat = lat;
    }

    @Override
    public String toString() {
        return this.lng + "," + this.lat;
    }

    /**
     * 计算两个坐标之间的距离
     *
     * @param dest 目标坐标
     * @return 米为单位
     */
    public double distance(Axis dest) {
        // 维度
        double lat1 = (Math.PI / 180) * this.lat;
        double lat2 = (Math.PI / 180) * dest.lat;
        // 经度
        double lon1 = (Math.PI / 180) * this.lng;
        double lon2 = (Math.PI / 180) * dest.lng;

        // 两点间距离 km
        double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R;
        return d * 1000;
    }
}
